﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Collections;
using Catering.View_Operaction;

namespace Catering.Catering_Page.richang
{
    public partial class cbsglInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(cbsglInfo));
            if (!IsPostBack)
            {
                this.LoadData();
                this.LoadRep();
            }
        }

        //加载页面数据
        public void LoadData()
        {
            if (Session["UserID"] != null)
            {
                string userid = Session["UserID"].ToString();
                int companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
                if (companyid > 0)
                {
                    DataTable dt_OperationArea = new Catering_Rules.OperationArea().GetAllListByCompanyID(companyid).Tables[0];
                    this.txt_info_zyq.LoadDrop(dt_OperationArea, "id", "AreaName");
                }
            }
        }

        //加载Rep
        public void LoadRep()
        {
            string strwhere = "CID=" + Request.QueryString["uid"].ToString();
            int Count = new Catering_Rules.ContractorInfo().GetCountBywhere(strwhere);
            this.AspNetPager1.RecordCount = Count;
            DataTable dt_Personnel = new Catering_Rules.ContractorInfo().GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, strwhere).Tables[0];
            this.Rep_Personnel.DataSource = dt_Personnel;
            this.Rep_Personnel.DataBind();
        }

        //删除
        protected void Rep_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.ContractorInfo().Delete(delID))
                {
                    this.LoadRep();
                }
            }
        }

        //分页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.LoadRep();
        }


        //获取人员信息
        [Ajax.AjaxMethod]
        public DataTable GetPersonnel(string cid)
        {
            return new Catering_Rules.ContractorInfo().GetList("id=" + cid).Tables[0];
        }

        //添加人员
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Catering_Model.ContractorInfo md = new Catering_Model.ContractorInfo();
            md.id = new Catering_Rules.ContractorInfo().GetMaxId();
            md.CID = Convert.ToInt32(Request.QueryString["uid"].ToString());
            md.CName = this.txt_info_name.Text.Trim();
            md.GroupID = this.txt_info_zyq.SelectedValue;
            md.CardNo = this.txt_info_kh.Text.Trim();
            md.JobCard = this.txt_info_zjmc.Text.Trim();
            md.Effective = this.txt_info_zjqj.Text.Trim();
            //md.EffectiveEnd = this.txt_info_zjqj_end.Text.Trim();----------------------------------------------------
            md.Tel = this.txt_info_tel.Text.Trim();
            //md.EffectiveArea = this.txt_info_yxqj.Text.Trim();--------------------------------------------------------
            new Catering_Rules.ContractorInfo().Add(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！');</script>");
            this.LoadRep();
            this.ClearTxt();
        }

        //修改人员
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Catering_Model.ContractorInfo md = new Catering_Model.ContractorInfo();
            md.id = Convert.ToInt32(this.HidID.Value);
            md.CID = Convert.ToInt32(Request.QueryString["uid"].ToString());
            md.CName = this.txt_info_name.Text.Trim();
            md.GroupID = this.txt_info_zyq.SelectedValue;
            md.CardNo = this.txt_info_kh.Text.Trim();
            md.JobCard = this.txt_info_zjmc.Text.Trim();
            md.Effective = this.txt_info_zjqj.Text.Trim();
            //md.EffectiveEnd = this.txt_info_zjqj_end.Text.Trim();--------------------------------
            md.Tel = this.txt_info_tel.Text.Trim();
            //md.EffectiveArea = this.txt_info_yxqj.Text.Trim();--------------------------------------
            new Catering_Rules.ContractorInfo().Update(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！');</script>");
            this.LoadRep();
            this.ClearTxt();
        }

        //清空控件
        public void ClearTxt()
        {
            this.txt_info_name.Text = "";
            this.txt_info_kh.Text = "";
            this.txt_info_zjmc.Text = "";
            this.txt_info_zjqj.Text = "";
            this.txt_info_zjqj_end.Text = "";
            this.txt_info_tel.Text = "";
            this.txt_info_yxqj.Text = "";
        }
    }
}